<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\Employee;
use App\Models\Organization;
use App\Models\PersonnelDynamics;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class OrganizationController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];


        if ( $request->organization_id) {
            $organization = Organization::findOrFail($request->organization_id);
            $organization->mtime = time();
        } else {
            $organization = new Organization();
            $organization->ctime = time();
        }
        $organization->organization_name = $request->organization_name;
        $organization->company_id = $company_id;
        $organization->parent_organization_id = $request->parent_organization_id;
        $organization->organization_code = $request->organization_code;
        $organization->organization_short_name = $request->organization_short_name;
        $organization->organization_type = $request->organization_type;
        $organization->organization_superior = $request->organization_superior;
        $organization->organization_leader = $request->organization_leader;
        $organization->organization_number = $request->organization_number;
        $organization->start_date = $request->start_date;
        $organization->organization_subleader = $request->organization_subleader;
        $organization->organization_description = $request->organization_description;
        $organization->organization_functon = $request->organization_functon;
        $organization->uid = $uid;

        $organization->save();

        return showError(Error::ERR_OK,['organization_id'=>$organization->organization_id]);

    }

    public function show(Request $request) {
        $organization = Organization::findOrFail($request->organization_id);
        return showError(Error::ERR_OK,['organization'=>$organization]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];
        $name = $request->input('name');

//        $code = $request->input('code');
//        if (!empty($code))
//        {
//            $where['code'] = $code;
//        }
//
//        $where['company_id'] = $company_id;
//        $where['status'] = 1;
//        $limit = $request->limit?:'20';

//        $list = Organization::getList($where,$limit);
        $list = Organization::where('company_id',$company_id)->where($where)->where('status',1);
        if (!empty($name))
        {
            $list->where('organization_name','like','%'.$name.'%');
        }
        $list = $list->get();

//        if ($limit == 20) {
        foreach ($list as $k => &$v) {

            $v->employee_num = Employee::where('organization_id',$v->organization_id)->where('company_id',$company_id)->where('employee_status','!=','离职')->where('status',1)->count();
            $v->employee_formal_num = Employee::where('organization_id',$v->organization_id)->where('company_id',$company_id)->where('employee_status','正式')->where('status',1)->count();
            $v->employee_noformal_num = $v->employee_num - $v->employee_formal_num;
        }
//        }

        $list2 = $this->getTree1($list);
        return showError(Error::ERR_OK,['list'=>$list2]);
    }

    function getTree1($data, $parent_id = 0)
    {
        $tree = array();
        foreach ($data as $k => $v) {

            if ($v["parent_organization_id"] == $parent_id) {
                unset($data[$k]);
                if (!empty($data)) {
                    $children = $this->getTree1($data, $v["organization_id"]);
                    if (!empty($children)) {
                        $v["children"] = $children;
                    }
                }
                $tree[] = $v;
            }
        }
        return $tree;
    }

}
